package com.rock.code.report.overview.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.report.overview.service.JbKpiOverviewService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* 监控
*
* @author xubh
* @date 2018/01/21
*/
@Controller
@RequestMapping("/KpiOverview")
public class JbKpiOverviewController extends BaseController {

    /**
    * The JbKpiOverviewService
    */
    @Resource
    private JbKpiOverviewService jbKpiOverviewService;

    @RequestMapping("")
    public String overviewPage(){

        return "overview/overView";
    }


    /**
     * queryPlatformHealth 平台分类健康度
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryPlatformHealth", method = RequestMethod.POST)
    public HashMap queryPlatformHealth() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List plist=jbKpiOverviewService.queryPlatformHealthDegree();//平台分类健康度
            resultMap.put("plist",plist);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryBusinessHealth 业务分类健康度
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryBusinessHealth", method = RequestMethod.POST)
    public HashMap queryBusinessHealth() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List blist=jbKpiOverviewService.queryBusinessHealthDegree();//业务分类健康度
            resultMap.put("blist",blist);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryTotalHealth 总健康度
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryTotalHealth", method = RequestMethod.POST)
    public HashMap queryTotalHealth() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List tlist=jbKpiOverviewService.queryTotalHealthDegree();//总健康度
            resultMap.put("tlist",tlist);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }
    /**
     * queryMonitorKpiCount 监控指标
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryMonitorKpiCount", method = RequestMethod.POST)
    public HashMap queryMonitorKpiCount() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbKpiOverviewService.queryMonitorKpiCount();//监控指标数量
            List mlist=jbKpiOverviewService.queryMonitorKpi();//监控指标
            resultMap.put("list",list);
            resultMap.put("mlist",mlist);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * alarmTendencyContrast 最近24小时告警趋势
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/alarmTendencyContrast", method = RequestMethod.POST)
    public HashMap alarmTendencyContrast() throws Exception {
        HashMap resultMap=new HashMap();
        List packList=new ArrayList();
        List list=new ArrayList();
        String start_time="";
        String end_time="";
        try{
            Calendar cal = Calendar.getInstance ();
            for(int i=1;i<=24;i++){
                if(i==1){
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                    String drtime=dateFormat1.format(cal.getTime());
                    packList.add(drtime.substring(11));
                    end_time=drtime+":59:59";

                }else {
                    cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                    String drtime = dateFormat1.format(cal.getTime());
                    packList.add(drtime.substring(11));
                    if(i==24){
                        start_time=drtime+":00:00";
                    }
                }
            }
            List yzList=jbKpiOverviewService.queryAlarmTendencyContrastList(start_time,end_time,"1");
            List zyList=jbKpiOverviewService.queryAlarmTendencyContrastList(start_time,end_time,"2");
            List ybList=jbKpiOverviewService.queryAlarmTendencyContrastList(start_time,end_time,"3");
            HashMap yzMap=new HashMap();
            HashMap zyMap=new HashMap();
            HashMap ybMap=new HashMap();
            if(yzList!=null&&yzList.size()>0){
                for(int a=0;a<yzList.size();a++){
                    HashMap yzmap=(HashMap)yzList.get(a);
                    String time=yzmap.get("GENERANT_TIME").toString();
                    String gj_count=yzmap.get("GJ_COUNT").toString();
                    yzMap.put(time.substring(11),gj_count);
                }
            }
            if(zyList!=null&&zyList.size()>0){
                for(int b=0;b<zyList.size();b++){
                    HashMap zymap=(HashMap)zyList.get(b);
                    String time=zymap.get("GENERANT_TIME").toString();
                    String gj_count=zymap.get("GJ_COUNT").toString();
                    zyMap.put(time.substring(11),gj_count);
                }
            }
            if(ybList!=null&&ybList.size()>0){
                for(int c=0;c<ybList.size();c++){
                    HashMap ybmap=(HashMap)ybList.get(c);
                    String time=ybmap.get("GENERANT_TIME").toString();
                    String gj_count=ybmap.get("GJ_COUNT").toString();
                    ybMap.put(time.substring(11),gj_count);
                }
            }
            for(int j=packList.size()-1;j>=0;j--){
                String hour=packList.get(j).toString();
                String yz_count = yzMap.get(hour)==null?"0":yzMap.get(hour).toString();
                String zy_count = zyMap.get(hour)==null?"0":zyMap.get(hour).toString();
                String yb_count = ybMap.get(hour)==null?"0":ybMap.get(hour).toString();
                HashMap map=new HashMap();
                map.put("hour",hour);
                map.put("yz_count",yz_count);
                map.put("zy_count",zy_count);
                map.put("yb_count",yb_count);
                list.add(map);
            }
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 查询资源指标数量
     *
     * @return
     * @throws Exception the exception
     * @author zhaowei
     * @date 2018/05/17
     */
    @RequestMapping(value = "/queryResourcceTargetOverview" ,method = RequestMethod.GET)
    @ResponseBody
    public List queryResourcceTargetOverviewController(){
        List list = new ArrayList();
        try{
            list=jbKpiOverviewService.queryResourcceTargetOverviewService();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 查询资源指标数量表格
     *
     * @return
     * @throws Exception the exception
     * @author zhaowei
     * @date 2018/05/17
     */
    @RequestMapping(value = "/queryResourceClassOverview",method = RequestMethod.POST)
    @ResponseBody
    public Map queryResourceClassOverviewController(){
        List list = new ArrayList();
        HashMap resultMap=new HashMap();
        try{
            list=jbKpiOverviewService.queryResourceClassOverviewService();
            List count=jbKpiOverviewService.queryResourcceTargetOverviewService();
            resultMap.put("list",list);
            resultMap.put("count",count);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 查询资源使用情况
     *
     * @return
     * @throws Exception the exception
     * @author zhaowei
     * @date 2018/05/17
     */
    @RequestMapping(value = "/queryResourceUseOverview",method = RequestMethod.POST)
    @ResponseBody
    public List queryResourceUseOverviewController(){
        List list = new ArrayList();
        try{
            list=jbKpiOverviewService.queryResourceUseOverviewService();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 查询巡检资源
     *
     * @return
     * @throws Exception the exception
     * @author zhaowei
     * @date 2018/05/17
     */
    @RequestMapping(value = "/queryInspectOverview",method = RequestMethod.POST)
    @ResponseBody
    public HashMap queryInspectOverviewController(){
        HashMap hashMap=new HashMap();
        try{
            hashMap=jbKpiOverviewService.queryInspectOverviewService();
        }catch (Exception e){
            e.printStackTrace();
        }
        return hashMap;
    }


}
